home *** CD-ROM | disk | FTP | other *** search
/ Download Now 8 / Download Now V8.iso / Program / InternetTools / ApacheWebServer1.3.6 / apache_1_3_6_win32.exe / _SETUP.1 / logresolve.pl < prev    next >
Encoding:
Perl Script  |  1999-01-01  |  8.9 KB  |  265 lines

  1. #!/usr/local/bin/perl
  2.  
  3. # ====================================================================
  4. # Copyright (c) 1995-1999 The Apache Group.  All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. #
  10. # 1. Redistributions of source code must retain the above copyright
  11. #    notice, this list of conditions and the following disclaimer. 
  12. #
  13. # 2. Redistributions in binary form must reproduce the above copyright
  14. #    notice, this list of conditions and the following disclaimer in
  15. #    the documentation and/or other materials provided with the
  16. #    distribution.
  17. #
  18. # 3. All advertising materials mentioning features or use of this
  19. #    software must display the following acknowledgment:
  20. #    "This product includes software developed by the Apache Group
  21. #    for use in the Apache HTTP server project (http://www.apache.org/)."
  22. #
  23. # 4. The names "Apache Server" and "Apache Group" must not be used to
  24. #    endorse or promote products derived from this software without
  25. #    prior written permission. For written permission, please contact
  26. #    apache@apache.org.
  27. #
  28. # 5. Products derived from this software may not be called "Apache"
  29. #    nor may "Apache" appear in their names without prior written
  30. #    permission of the Apache Group.
  31. #
  32. # 6. Redistributions of any form whatsoever must retain the following
  33. #    acknowledgment:
  34. #    "This product includes software developed by the Apache Group
  35. #    for use in the Apache HTTP server project (http://www.apache.org/)."
  36. #
  37. # THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  38. # EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  40. # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  41. # ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  42. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  43. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  44. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  45. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  46. # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  47. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  48. # OF THE POSSIBILITY OF SUCH DAMAGE.
  49. # ====================================================================
  50. #
  51. # This software consists of voluntary contributions made by many
  52. # individuals on behalf of the Apache Group and was originally based
  53. # on public domain software written at the National Center for
  54. # Supercomputing Applications, University of Illinois, Urbana-Champaign.
  55. # For more information on the Apache Group and the Apache HTTP server
  56. # project, please see <http://www.apache.org/>.
  57.  
  58. # logresolve.pl
  59. #
  60. # v 1.2 by robh @ imdb.com
  61. # usage: logresolve.pl <infile >outfile
  62. #
  63. # input = Apache/NCSA/.. logfile with IP numbers at start of lines
  64. # output = same logfile with IP addresses resolved to hostnames where
  65. #  name lookups succeeded.
  66. #
  67. # this differs from the C based 'logresolve' in that this script
  68. # spawns a number ($CHILDREN) of subprocesses to resolve addresses
  69. # concurrently and sets a short timeout ($TIMEOUT) for each lookup in
  70. # order to keep things moving quickly.
  71. #
  72. # the parent process handles caching of IP->hostnames using a Perl hash
  73. # it also avoids sending the same IP to multiple child processes to be
  74. # resolved multiple times concurrently.
  75. #
  76. # Depending on the settings of $CHILDREN and $TIMEOUT you should see
  77. # significant reductions in the overall time taken to resolve your
  78. # logfiles. With $CHILDREN=40 and $TIMEOUT=5 I've seen 200,000 - 300,000
  79. # logfile lines processed per hour compared to ~45,000 per hour
  80. # with 'logresolve'.
  81. #
  82. # I haven't yet seen any noticable reduction in the percentage of IPs
  83. # that fail to get resolved. Your mileage will no doubt vary. 5s is long
  84. # enough to wait IMO.
  85. #
  86. # Known to work with FreeBSD 2.2
  87. # Known to have problems with Solaris
  88. #
  89. # 980417 - use 'sockaddr_un' for bind/connect to make the script work
  90. #  with linux. Fix from Luuk de Boer <luuk_de_boer@pi.net>
  91.  
  92. require 5.004;
  93.  
  94. $|=1;
  95.  
  96. use FileHandle;
  97. use Socket;
  98.  
  99. use strict;
  100. no strict 'refs';
  101.  
  102. use vars qw($PROTOCOL);
  103. $PROTOCOL = 0;
  104.  
  105. my $CHILDREN = 40;
  106. my $TIMEOUT  = 5;
  107.  
  108. my $filename;
  109. my %hash = ();
  110. my $parent = $$;
  111.  
  112. my @children = ();
  113. for (my $child = 1; $child <=$CHILDREN; $child++) {
  114.     my $f = fork();    
  115.     if (!$f) {
  116.         $filename = "./.socket.$parent.$child";
  117.         if (-e $filename) { unlink($filename) || warn "$filename .. $!\n";}
  118.         &child($child);
  119.         exit(0);
  120.     }
  121.     push(@children, $f);
  122. }
  123.  
  124. &parent;
  125. &cleanup;
  126.  
  127. ## remove all temporary files before shutting down
  128. sub cleanup {
  129.      # die kiddies, die
  130.     kill(15, @children);
  131.     for (my $child = 1; $child <=$CHILDREN; $child++) {
  132.         if (-e "./.socket.$parent.$child") {
  133.             unlink("./.socket.$parent.$child")
  134.                 || warn ".socket.$parent.$child $!";
  135.         }
  136.     }
  137. }
  138.     
  139. sub parent {
  140.     # Trap some possible signals to trigger temp file cleanup
  141.     $SIG{'KILL'} = $SIG{'INT'} = $SIG{'PIPE'} = \&cleanup;
  142.  
  143.     my %CHILDSOCK;
  144.     my $filename;
  145.  
  146.      ## fork child processes. Each child will create a socket connection
  147.      ## to this parent and use an unique temp filename to do so.
  148.     for (my $child = 1; $child <=$CHILDREN; $child++) {
  149.         $CHILDSOCK{$child}= FileHandle->new;
  150.  
  151.         if (!socket($CHILDSOCK{$child}, AF_UNIX, SOCK_STREAM, $PROTOCOL)) {
  152.             warn "parent socket to child failed $!";
  153.         }
  154.         $filename = "./.socket.$parent.$child";
  155.         my $response;
  156.         do {
  157.             $response = connect($CHILDSOCK{$child}, sockaddr_un($filename));
  158.             if ($response != 1) {
  159.                 sleep(1);
  160.             }                       
  161.         } while ($response != 1);
  162.         $CHILDSOCK{$child}->autoflush;
  163.     }
  164.     ## All child processes should now be ready or at worst warming up 
  165.  
  166.     my (@buffer, $child, $ip, $rest, $hostname, $response);
  167.      ## read the logfile lines from STDIN
  168.     while(<STDIN>) {
  169.         @buffer = ();    # empty the logfile line buffer array.
  170.         $child = 1;        # children are numbered 1..N, start with #1
  171.  
  172.         # while we have a child to talk to and data to give it..
  173.         do {
  174.             push(@buffer, $_);                    # buffer the line
  175.             ($ip, $rest) = split(/ /, $_, 2);    # separate IP form rest
  176.  
  177.             unless ($hash{$ip}) {                # resolve if unseen IP
  178.                 $CHILDSOCK{$child}->print("$ip\n"); # pass IP to next child
  179.                 $hash{$ip} = $ip;                # don't look it up again.
  180.                 $child++;
  181.             }
  182.         } while (($child < ($CHILDREN-1)) and ($_ = <STDIN>));
  183.  
  184.          ## now poll each child for a response
  185.         while (--$child > 0) { 
  186.             $response = $CHILDSOCK{$child}->getline;
  187.             chomp($response);
  188.              # child sends us back both the IP and HOSTNAME, no need for us
  189.              # to remember what child received any given IP, and no worries
  190.              # what order we talk to the children
  191.             ($ip, $hostname) = split(/\|/, $response, 2);
  192.             $hash{$ip} = $hostname;
  193.         }
  194.  
  195.          # resolve all the logfiles lines held in the log buffer array..
  196.         for (my $line = 0; $line <=$#buffer; $line++) {
  197.              # get next buffered line
  198.             ($ip, $rest) = split(/ /, $buffer[$line], 2);
  199.              # separate IP from rest and replace with cached hostname
  200.             printf STDOUT ("%s %s", $hash{$ip}, $rest);
  201.         }
  202.     }
  203. }
  204.  
  205. ########################################
  206.  
  207. sub child {
  208.      # arg = numeric ID - how the parent refers to me
  209.     my $me = shift;
  210.  
  211.      # add trap for alarm signals.
  212.     $SIG{'ALRM'} = sub { die "alarmed"; };
  213.  
  214.      # create a socket to communicate with parent
  215.     socket(INBOUND, AF_UNIX, SOCK_STREAM, $PROTOCOL)
  216.         || die "Error with Socket: !$\n";
  217.     $filename = "./.socket.$parent.$me";
  218.     bind(INBOUND, sockaddr_un($filename))
  219.         || die "Error Binding $filename: $!\n";
  220.     listen(INBOUND, 5) || die "Error Listening: $!\n";
  221.  
  222.     my ($ip, $send_back);
  223.     my $talk = FileHandle->new;
  224.  
  225.      # accept a connection from the parent process. We only ever have
  226.      # have one connection where we exchange 1 line of info with the
  227.      # parent.. 1 line in (IP address), 1 line out (IP + hostname).
  228.     accept($talk, INBOUND) || die "Error Accepting: $!\n";
  229.      # disable I/O buffering just in case
  230.     $talk->autoflush;
  231.      # while the parent keeps sending data, we keep responding..
  232.     while(($ip = $talk->getline)) {
  233.         chomp($ip);
  234.          # resolve the IP if time permits and send back what we found..
  235.         $send_back = sprintf("%s|%s", $ip, &nslookup($ip));
  236.         $talk->print($send_back."\n");
  237.     }
  238. }
  239.  
  240. # perform a time restricted hostname lookup.
  241. sub nslookup {
  242.      # get the IP as an arg
  243.     my $ip = shift;
  244.     my $hostname = undef;
  245.  
  246.      # do the hostname lookup inside an eval. The eval will use the
  247.      # already configured SIGnal handler and drop out of the {} block
  248.      # regardless of whether the alarm occured or not.
  249.     eval {
  250.         alarm($TIMEOUT);
  251.         $hostname = gethostbyaddr(gethostbyname($ip), AF_INET);
  252.         alarm(0);
  253.     };
  254.     if ($@ =~ /alarm/) {
  255.          # useful for debugging perhaps..
  256.         # print "alarming, isn't it? ($ip)";
  257.     }
  258.  
  259.      # return the hostname or the IP address itself if there is no hostname
  260.     $hostname ne "" ? $hostname : $ip;
  261. }
  262.  
  263.  
  264.